package de.ba.se.roboter.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import de.ba.se.roboter.controller.MainController;

/**
 * Das Hauptfenster der Anwendung.
 *  
 * @author Sebastian Tönse
 */
public class MainView extends JFrame {

	/** @see java.io.Serializable */
	private static final long serialVersionUID = -4349796125553629863L;
	
	private JMenu fileMenu;
	
	private JMenuBar mainMenuBar;
	
	private JMenuItem newMenuItem;
	
	private JMenuItem saveMenuItem;
	
	private JMenuItem exitMenuItem;
	
	private RobotPanel robotPanel;
	
	private ControlPanel controlPanel;
	
	private MainController controller;
	
	/**
	 * Konstruktor, der den Controller übergeben bekommt, der dieses Fenster verwaltet.
	 * @param controller Der steuernde Controller.
	 */
	public MainView(MainController controller) {
		this.controller = controller;
		setTitle("Robot");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(getMainMenuBar(), BorderLayout.NORTH);
		add(getControlPanel(), BorderLayout.WEST);
		final JScrollPane scrollPane = new JScrollPane(getRobotPanel());
		add(scrollPane, BorderLayout.CENTER);
		setPreferredSize(new Dimension(640, 480));
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	/**
	 * Datei-Menü.
	 * @return Das Element.
	 */
	public JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu("Datei");
			fileMenu.add(getNewMenuItem());
			fileMenu.add(getSaveMenuItem());
			fileMenu.addSeparator();
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * ...
	 * @return Das Element.
	 */
	public JMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new JMenuBar();
			mainMenuBar.add(getFileMenu());
		}
		return mainMenuBar;
	}

	/**
	 * ...
	 * @return Das Element.
	 */
	public JMenuItem getNewMenuItem() {
		if (newMenuItem == null) {
			newMenuItem = new JMenuItem("Neu");
			newMenuItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					controller.showConfigView();
				}
			});
		}
		return newMenuItem;
	}

	/**
	 * ...
	 * @return Das Element.
	 */
	public JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem("Schließen");
			exitMenuItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return exitMenuItem;
	}
	
	/**
	 * ...
	 * @return Das Element.
	 */
	public RobotPanel getRobotPanel() {
		if (robotPanel == null) {
			robotPanel = new RobotPanel();
		}
		return robotPanel;
	}
	
	/**
	 * ...
	 * @return Das Element.
	 */
	public JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem("Speichern");
			saveMenuItem.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					final JFileChooser chooser = new JFileChooser();
					chooser.setFileFilter(new FileNameExtensionFilter("PNG-Bild", "png"));
					final int ret =chooser.showSaveDialog((Component) e.getSource());
					if (ret == JFileChooser.APPROVE_OPTION) {
						File f = chooser.getSelectedFile();
						if (!f.getName().endsWith(".png")) {
							f = new File(f.getAbsolutePath() + ".png");
							chooser.setSelectedFile(f);
						}
						controller.saveImage(chooser.getSelectedFile());
					}
				}
			});
		}
		return saveMenuItem;
	}
	
	/**
	 * Gibt das {@link ControlPanel} zurück.
	 * @return Das Element.
	 */
	public ControlPanel getControlPanel() {
		if (controlPanel == null) {
			controlPanel = new ControlPanel(this.controller);
		}
		return controlPanel;
	}
}
